using System;
using System.Diagnostics;
using Needle.Utils;

namespace Needle.Core.Lifetime {
    /// <summary>
    /// Base class for a tracking policy attribute.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public abstract class TrackingPolicyAttribute : Attribute, ITrackingPolicy {

        /// <summary>
        /// returns all trackingpolicy attributes for the given type.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static TrackingPolicyAttribute GetFor(Type t) {
            Ensure.NotNull(t, "t");

            TrackingPolicyAttribute[] policies = Attributes.Get<TrackingPolicyAttribute>(t);
            if (policies.Length == 0) {
                return null;
            }
            if (policies.Length > 1) {
                Debug.WriteLine("Multiple tracking policies set on " + t.FullName + ", picking one at random.");
            }
            return policies[0];
        }
        /// <summary>
        /// When implemented by subclasses this method should return 
        /// an IReference object implementing the tracking policy.
        /// </summary>
        public abstract IReference Apply(object instance);

    }

}
